import AppContext, { AppState } from "@/context/app-context";
import { useContext } from "react";

function useAppContext<TSelector>(selector: (state: AppState) => TSelector): TSelector;
function useAppContext(): AppState;
function useAppContext<TSelector>(selector?: (state: AppState) => TSelector) {
  const context = useContext(AppContext)
  if (!context) {
    throw new Error('AppActionContext 不存在')
  }
  if (!selector) {
    return context
  }
  return selector(context)
}

export default useAppContext